package com.hit.server.biz.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hit.common.core.util.HitConvertUtils;
import com.hit.common.core.util.R;
import com.hit.common.log.annotation.SysLog;
import com.hit.server.api.entity.CheckTypeEntity;
import com.hit.server.api.dto.CheckTypeQueryDTO;
import com.hit.server.api.dto.CheckTypeSaveDTO;
import com.hit.server.api.dto.CheckTypeUpdateDTO;
import com.hit.server.api.vo.CheckTypeVO;
import com.hit.server.api.service.CheckTypeService;
import com.hit.common.security.annotation.HasPermission;
import com.hit.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 检测类型
 *
 * @author kehanjiang
 * @date 2025-04-15 11:21:18
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/checkType" )
@Tag(description = "checkType" , name = "检测类型管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CheckTypeController {

    private final  CheckTypeService checkTypeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param dto 检测类型
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R<Page<CheckTypeVO>> getCheckTypePage(@ParameterObject Page page, @ParameterObject CheckTypeQueryDTO dto) {
	    dto = HitConvertUtils.emptyToNull(dto);
        return R.ok(checkTypeService.getCheckTypePage(page, dto));
    }


    /**
     * 通过id查询检测类型
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    public R<CheckTypeVO> getById(@PathVariable("id" ) Long id) {
        return R.ok(checkTypeService.getCheckTypeById(id));
    }

    /**
     * 新增检测类型
     * @param dto 检测类型
     * @return R
     */
    @Operation(summary = "新增检测类型" , description = "新增检测类型" )
    @SysLog("新增检测类型" )
    @PostMapping
    public R<Boolean> save(@RequestBody CheckTypeSaveDTO dto) {
        return R.ok(checkTypeService.saveCheckType(dto));
    }

    /**
     * 修改检测类型
     * @param dto 检测类型
     * @return R
     */
    @Operation(summary = "修改检测类型" , description = "修改检测类型" )
    @SysLog("修改检测类型" )
    @PutMapping
    public R<Boolean> updateById(@RequestBody CheckTypeUpdateDTO dto) {
        return R.ok(checkTypeService.updateCheckTypeById(dto));
    }

    /**
     * 通过id删除检测类型
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除检测类型" , description = "通过id删除检测类型" )
    @SysLog("通过id删除检测类型" )
    @DeleteMapping
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(checkTypeService.removeCheckTypeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param checkType 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<CheckTypeEntity> export(CheckTypeEntity checkType,@RequestParam List<Long> ids) {
        return checkTypeService.list(Wrappers.lambdaQuery(checkType).in(ArrayUtil.isNotEmpty(ids), CheckTypeEntity::getId, ids));
    }
}